/*
 *  UpdateSystem.cpp
 *  Milovo04alfa06
 *
 *  Created by Luca on 17/04/07.
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
 *
 */

#include "MilovoTables.h"

void UpdateSystem(MilinitParams &tmpInitParam, Agents *tmpTabAgents, Recipes *tmpTabRecipes, Artifacts *tmpTabArtifacts, 
						ScaffoldingStructure *tmpTabScaffoldigStructure, Requests *tmpTabRequests, MilovoEnvironment &tmpMiloEnv)
{
	if(tmpMiloEnv.GetDebugLevel() >= 1)
		cout << "UpdateArtifacts [begin]" << endl;
	
	Mil_Int tmpRawMaterialID = 1;
	for(vector<Mil_Int>::iterator VectRawMaterialsIter = tmpInitParam.VectRawMaterials.begin(); VectRawMaterialsIter != tmpInitParam.VectRawMaterials.end(); VectRawMaterialsIter++)
	{
		Artifacts::iterator tmpTabArtifactsIter = tmpTabArtifacts->find(tmpRawMaterialID);
		if((tmpMiloEnv.GetDebugLevel() >= 0) && (tmpTabArtifactsIter == tmpTabArtifacts->end()))
		{
			cout << "Error in UpdateArtifacts function: cannot find tmpTabArtifactsIter in tmpTabArtifacts" << endl;
			exit(EXIT_FAILURE);
		}
		tmpTabArtifactsIter->second.SetAmount(1000000);
		tmpRawMaterialID++;
	}
	
	for(Artifacts::iterator tmpTabArtifactsIter = tmpTabArtifacts->begin(); tmpTabArtifactsIter != tmpTabArtifacts->end(); tmpTabArtifactsIter++)
	{
		if((tmpTabArtifactsIter->second.GetID() >= tmpRawMaterialID) && (tmpTabArtifactsIter->second.GetNumBuyers() == 0))
			tmpTabArtifactsIter->second.SetStatus(tmpTabArtifactsIter->second.GetStatus() - 1);
		// set numbero of sells on zero level for next step
		tmpTabArtifactsIter->second.SetNumBuyers(0);
	}
	
	tmpMiloEnv.SetNumInnovationFailed(0);
	tmpMiloEnv.SetNumInnovationSucceded(0);
	tmpMiloEnv.SetNumRecipesDeadByProduction(0);
	tmpMiloEnv.SetNumRecipesDeadByMarket(0);
//	tmpMiloEnv.SetNumAgentsDeadByZeroRecipes(0);
//	tmpMiloEnv.SetNumAgentsDeadByMarket(0);
	
	for(Agents::iterator tmpTabAgentsIter = tmpTabAgents->begin(); tmpTabAgentsIter != tmpTabAgents->end(); tmpTabAgentsIter++)
	{
		tmpTabAgentsIter->second.SetNumTimeSelected(0);
		tmpTabAgentsIter->second.SetNumInnovationSucceded(0);
		tmpTabAgentsIter->second.SetNumInnovationFailed(0);
		tmpTabAgentsIter->second.SetNumProjectsSucceded(0);
		tmpTabAgentsIter->second.SetNumProjectsFailed(0);
		tmpTabAgentsIter->second.SetIDTempRequest(0);
	}
	// increment the presence counter in scaffolding structures
	ScaffoldingStructure::iterator tmpTabScaffoldigStructureIter = tmpTabScaffoldigStructure->begin();
	while(tmpTabScaffoldigStructureIter != tmpTabScaffoldigStructure->end())
	{
		Agents::iterator tmpTabAgentsIter = tmpTabAgents->find(tmpTabScaffoldigStructureIter->second.GetIDAgent());
		if(tmpTabAgentsIter != tmpTabAgents->end())
		{
			tmpTabScaffoldigStructureIter->second.IncCounter();
			tmpTabScaffoldigStructureIter++;
		}
		else
		{
			ScaffoldingStructure::iterator tmpScaffoldingElementToEraseIter = tmpTabScaffoldigStructureIter;
			tmpTabScaffoldigStructureIter++;
			tmpTabScaffoldigStructure->erase(tmpScaffoldingElementToEraseIter);
		}
	}
	
	Requests::iterator tmpTabRequestsIter = tmpTabRequests->begin();
	while(tmpTabRequestsIter != tmpTabRequests->end())
	{
		if((tmpTabRequestsIter->second.GetChecked() == true) && (tmpTabRequestsIter->second.GetStepChecked() < tmpMiloEnv.GetCurrentStep()))
		{
			Mil_Int IDRequestToErase = tmpTabRequestsIter->second.GetID();
			tmpTabRequestsIter++;
			tmpTabRequests->erase(IDRequestToErase);
		}
		else tmpTabRequestsIter++;
	}
	
	if(tmpMiloEnv.GetDebugLevel() >= 1)
		cout << "UpdateArtifacts [end]" << endl;
}

